package com.yonyou.marketing.app.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.AppResponse;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.yonyou.gmmc.common.annotation.ParamValid;
import com.yonyou.marketing.app.api.constant.AppConstant;
import com.yonyou.marketing.app.api.entity.EvaluationResultPO;
import com.yonyou.marketing.app.api.service.AppRepairRecordService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator
 * @date 2017年6月30日
 */
@RestController
@RequestMapping(value = AppConstant.BASE_PATH + "/" + AppConstant.VERSION)
@Api("APP维修履历API")
public class AppRepairRecordController {

	private Logger logger = Logger.getLogger(AppRepairRecordController.class);

	@Autowired
	private AppRepairRecordService appRepairRecordService;

	/**
	 * 查询维修履历
	 * 
	 * @author Administrator
	 * @date 2017年11月3日
	 * @return
	 */
	@RequestMapping(value = "/maintenanceorder", method = RequestMethod.POST)
	@ApiOperation(value = "查询维修履历", notes = "查询维修履历")
	public AppResponse<Map<String, Object>> maintenanceorder(
			@RequestParam(value = "userId", required = true) String userId,
			@RequestParam(value = "vin", required = false) String vin) {
		logger.info("AppRepairRecordController.maintenanceorder==>start...");
		try {
			Map<String, Object> map = appRepairRecordService.maintenanceorder(userId, vin);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppRepairRecordController.maintenanceorder==>end...");
		}
	}

	/**
	 * 查询维修履历详情
	 * 
	 * @author Administrator
	 * @date 2017年11月3日
	 * @return
	 */
	@RequestMapping(value = "/maintenanceorderDetail", method = RequestMethod.POST)
	@ApiOperation(value = "查询维修履历详情", notes = "查询维修履历详情")
	public AppResponse<Map<String, Object>> maintenanceorderDetail(
			@RequestParam(value = "maintenanceWorkOrderNum", required = false) String orderNum,
			@RequestParam(value = "dealerCode", required = false) String dealerCode) {
		logger.info("AppRepairRecordController.maintenanceorderDetail==>start...");
		try {
			Map<String, Object> map = appRepairRecordService.maintenanceorderDetail(orderNum, dealerCode);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppRepairRecordController.maintenanceorderDetail==>end...");
		}
	}

	/**
	 * 查看工单评价
	 * 
	 * @author Administrator
	 * @date 2017年11月3日
	 * @return
	 */
	@RequestMapping(value = "/findEvaluation", method = RequestMethod.POST)
	@ApiOperation(value = "查看工单评价", notes = "查看工单评价")
	public AppResponse<EvaluationResultPO> addEvaluation(@RequestParam(required = true, defaultValue = "") Integer type,
			@RequestParam(required = true, defaultValue = "") String orderNumber,
			@RequestParam(required = true, defaultValue = "") String dealerCode,
			@RequestParam(required = true, defaultValue = "") Integer evaType, // 0未评价
																				// 1已评价
			@RequestParam(value = "userId", required = true) String userId) {
		logger.info("AppRepairRecordController.addEvaluation==>start...");
		try {
			EvaluationResultPO map = appRepairRecordService.addEvaluation(type, orderNumber + dealerCode, evaType,
					userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppRepairRecordController.addEvaluation==>end...");
		}
	}

	/**
	 * 校验是否存在可评价工单
	 * 
	 * @author Administrator
	 * @date 2017年11月3日
	 * @return
	 */
	@RequestMapping(value = "/checkEvaluation", method = RequestMethod.POST)
	@ApiOperation(value = "校验是否存在可评价工单", notes = "校验是否存在可评价工单")
	public AppResponse<Map<String, Object>> checkEvaluation(
			@RequestParam(required = true, defaultValue = "") Integer type,
			@RequestParam(required = true, defaultValue = "") String orderNumber,
			@RequestParam(value = "userId", required = true) String userId) {
		logger.info("AppRepairRecordController.checkEvaluation==>start...");
		try {
			Map<String, Object> map = appRepairRecordService.checkEvaluation(type, orderNumber, userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppRepairRecordController.checkEvaluation==>end...");
		}
	}

	/**
	 * 提交工单评价
	 * 
	 * @author Administrator
	 * @date 2017年11月3日
	 * @return
	 */
	@RequestMapping(value = "/saveEvaluation", method = RequestMethod.POST)
	@ApiOperation(value = "提交工单评价", notes = "提交工单评价")
	public AppResponse<Map<String, Object>> saveEvaluation(@RequestParam Map<String, Object> param) {
		logger.info("AppRepairRecordController.saveEvaluation==>start...");
		try {
			Map<String, Object> map = appRepairRecordService.saveEvaluation(param);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppRepairRecordController.saveEvaluation==>end...");
		}
	}

	/**
	 * 获取优惠券列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/ticketList", method = RequestMethod.POST)
	@ApiOperation(value = "获取优惠券列表", notes = "获取优惠券列表")
	@ParamValid
	public AppResponse<PageInfo<Map<String, Object>>> getTicketList(
			@RequestParam(required = true, defaultValue = "0") Integer pageNum,
			@RequestParam(required = true, defaultValue = "10") Integer pageSize,
			@RequestParam(required = true, defaultValue = "0") Integer status,
			@RequestParam(value = "userId", required = true) Integer userId, HttpServletRequest request) {
		logger.info("TicketController.getTicketList==>start...");
		try {
			Map<String, Integer> map = new HashMap<>();
			map.put("pageNum", pageNum);
			map.put("pageSize", pageSize);
			map.put("status", status);
			map.put("userId", userId);
			PageInfo<Map<String, Object>> result = appRepairRecordService.getTicketList(map);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("TicketController.getTicketList==>end...");
		}
	}
}
